package elijah.base.net;

import android.content.Context;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import elijah.base.AppBase;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Nets {
    private static final String DEFAULT = "default_net_api";
    private static final Map<String, Object> netApiHashMap = new ConcurrentHashMap<>(5);


    public static synchronized <N> N request(Context context,Class<N> cls) {
        return get(cls, DEFAULT);
    }


    public static synchronized <N> N get(Class<N> cls) {
        return get(cls, DEFAULT);
    }

    public static synchronized <N> N get(Class<N> cls, String tag) {
        N netApi = (N) netApiHashMap.get(TextUtils.isEmpty(tag) ? DEFAULT : tag);
        if (netApi == null) {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("androidId", AppBase.get().getAndroidId());
            netApi = Nets.getNetProxy(AppBase.get().getApiBaseUrl(), cls, headers);
            netApiHashMap.put(TextUtils.isEmpty(tag) ? DEFAULT : tag, netApi);
        }
        return netApi;
    }

    private static <T> T getNetProxy(String baseUrl, final Class<T> service) {
        return createRetrofit(baseUrl, service, 5, null);
    }

    private static <T> T getNetProxy(String baseUrl, final Class<T> service, HashMap<String, String> headers) {
        return createRetrofit(baseUrl, service, 5, headers);
    }

    private static <T> T getNetProxy(String baseUrl, final Class<T> service, int second, HashMap<String, String> headers) {
        return createRetrofit(baseUrl, service, second, headers);
    }

    private static <T> T createRetrofit(String baseUrl, final Class<T> service, int connectTimeout, HashMap<String, String> headers) {
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectTimeout(connectTimeout, TimeUnit.SECONDS);
        if (headers != null && headers.values().size() > 0) {
            clientBuilder.addInterceptor(chain -> {
                Request original = chain.request();
                Request.Builder builder = original.newBuilder().method(original.method(), original.body());
                for (String key : headers.keySet()) {
                    String obj = headers.get(key);
                    if (!TextUtils.isEmpty(obj)) {
                        builder.addHeader(key, Objects.requireNonNull(obj));
                    }
                }
                return chain.proceed(builder.build());
            });

        }
        Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl).client(clientBuilder.build()).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();
        return (T) retrofit.create(service);
    }

}
